package be.kuleuven.mume.paginas;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.AgendaItem;
import be.kuleuven.mume.framework.Prof;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AgendaEditPagina extends Activity implements OnClickListener{
	public static final String AGENDA_ITEM = "AGENDA_ITEM";
	public static final String PROF = "PROF";
	
	private AgendaItem a;
	private Prof p;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agenda_edit_pagina);
		setTitle(R.string.agenda);
		
		Object item = getIntent().getExtras().get(AGENDA_ITEM);
		if(item != null) {
			a = (AgendaItem)item;
			setAgendaItem();
		} else {
			item = getIntent().getExtras().get(PROF);
			if(item != null){
				p = (Prof)item;
				a = new AgendaItem();
				a.setType("afspraak");
				a.setMet(p.getName());
				TextView title = (TextView)findViewById(R.id.agenda_title);
				String t = getString(R.string.agenda) + ":" + p.getName();
				title.setText(t);
			}else {
				a = new AgendaItem();
				a.setType("vrije_tijd");
				setTitle(R.string.nieuw_vrijetijd);
			}
		}
		
		Button bevestig = (Button)this.findViewById(R.id.agenda_bevestig);
		bevestig.setOnClickListener(this);
	}

	public void onClick(View v) {
        Bundle extras = new Bundle();
        try {
	        a = extractAgendaItem();
			
	        extras.putSerializable(AgendaItem.AGENDA_ID, a);
	
	        Intent mIntent = new Intent();
	        mIntent.putExtras(extras);
	        setResult(RESULT_OK, mIntent);
	        finish();
        }catch (NumberFormatException e){
        	Toast.makeText(this, R.string.numberError, 10).show();
		} catch (ParseException e1) {
			Toast.makeText(this, R.string.ParseError, 10).show();
		}
	}

	private AgendaItem extractAgendaItem() throws ParseException {		
		DateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm");
		EditText d = (EditText)this.findViewById(R.id.agenda_datum);
		EditText t = (EditText)this.findViewById(R.id.agenda_tijd);
		String dateTime = d.getText().toString() + t.getText().toString();
		a.setDatum(df.parse(dateTime));
		
		d= (EditText)findViewById(R.id.agenda_uur);
		int h = Integer.parseInt(d.getText().toString());
		a.setHours(h);
		
		d=(EditText)findViewById(R.id.agenda_min);
		int m = Integer.parseInt(d.getText().toString());
		a.setMinutes(m);
		
		d=(EditText)findViewById(R.id.agenda_locatie);
		a.setLocatie(d.getText().toString());
		
		d=(EditText)findViewById(R.id.agenda_omschrijving);
		a.setOmschrijving(d.getText().toString());
		
		return a;
	}
	
	private AgendaItem setAgendaItem(){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		setEditText(R.id.agenda_datum, df.format(a.getDatum()));
		df = new SimpleDateFormat("HH:mm");
		setEditText(R.id.agenda_tijd, df.format(a.getDatum()));
		setEditText(R.id.agenda_uur, a.getHours());
		setEditText(R.id.agenda_min, a.getMinutes());
		setEditText(R.id.agenda_locatie, a.getLocatie());
		setEditText(R.id.agenda_omschrijving, a.getOmschrijving());
		
		return a;
	}

	private void setEditText(int id, String text) {
		EditText d = (EditText)findViewById(id);
		d.setText(text);
		d.setEnabled(false);
	}
	
	private void setEditText(int id, int text) {
		setEditText(id, Integer.toString(text));
	}
}
